package com.handong.handserialprogram.module.loginoutring;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.KeyConstant;
import com.handong.handserialprogram.model.entities.CriminalEntity;
import com.handong.handserialprogram.module.base.BaseActivity;
import com.handong.handserialprogram.module.base.BaseView;
import com.handong.handserialprogram.module.singletask.CriminalListAdapter;
import com.handong.handserialprogram.utils.AppUtil;
import com.handong.handserialprogram.utils.SPUtil;
import com.handong.handserialprogram.utils.ViewUtil;

import java.util.List;

public class LoginOutRingActivity extends BaseActivity<LoginOutRingPresenter> implements BaseView {

    private LinearLayout mContentLayout;
    private RecyclerView mPersonList;
    protected String RingId;
    private LoginOutRingAdapter mListAdapter;
    private List<CriminalEntity> mCriminalEntityList;

    @Override
    protected int getContentViewID() {
        return R.layout.activity_login_out_ring;
    }

    @Override
    protected LoginOutRingPresenter createPresenter() {
        return new LoginOutRingPresenter(this);
    }

    /**
     * 显示犯人列表
     * @param list
     */
    protected void showCriminalList(List<CriminalEntity> list) {

        if (mListAdapter == null) {
            mCriminalEntityList=list;
            mListAdapter = new LoginOutRingAdapter(mCriminalEntityList, this);
            mPersonList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            mPersonList.setAdapter(mListAdapter);
            ((SimpleItemAnimator)mPersonList.getItemAnimator()).setSupportsChangeAnimations(false);
        }
        mContentLayout.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initViewByID() {
        final Context context=this;

        mContentLayout = (LinearLayout) findViewById(R.id.missionContentLayout);
        mPersonList = (RecyclerView) findViewById(R.id.missionPersonList);

        findView(R.id.headerOptionLayout).setOnClickListener(this);
        findView(R.id.missionStop).setOnClickListener(this);
    }


    @Override
    protected void initViewDataRes() {
        initHeaderLayout(false);
        //setOptionOnClick();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            RingId = bundle.getString(KeyConstant.LOGIN_OUT_RING_ID);
            mPresenter.CreateCriminal();
        }
        mPresenter.logoutAllFootRing();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headerBackLayout:
                finish();
                break;
            case R.id.missionStop:
//                if(!mPresenter.checkIsAnyCriminalEscape()) {
                    mPresenter.logoutAllFootRing();
//                }
//                else{
//                    AppUtil.showShortToast(this, "当前有设备为非正常状态或距离超过"+ SPUtil.getIntValue(SPUtil.STOP_MISSION_SET_DISTANCE)+"米，不允许结束任务！");
//                }
                break;
        }
    }

    /**
     * 更新距离
     */
    protected void refreshRingDistance(int index){
        mListAdapter.notifyItemChanged(index);
    }



    /**
     * 休眠脚环失败
     */
    protected void logoutFootRingError(final String id) {
        mPresenter.resumeCurrentFootRing();
        final Context context=this;
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content("休眠脚环失败，请重试(重试时请确保当前所有设备为正常状态且距离为"+SPUtil.getIntValue(SPUtil.STOP_MISSION_SET_DISTANCE)+"米以内)！")
                .negativeColorRes(R.color.dialog_bg)
                .negativeText("退出")
                .positiveText(R.string.dialog_positive_again)
                .positiveColorRes(R.color.back_ground_green)
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    public void onClick(@NonNull MaterialDialog paramAnonymousMaterialDialog, @NonNull DialogAction paramAnonymousDialogAction) {
                        mPresenter.logoutSuccess();

                    }
                })
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    public void onClick(@NonNull MaterialDialog paramAnonymousMaterialDialog, @NonNull DialogAction paramAnonymousDialogAction) {
                        if(!mPresenter.checkIsAnyCriminalEscape()) {
                            mPresenter.logoutAllFootRing();
                        }
                        else{
                            AppUtil.showShortToast(context, "当前有设备为非正常状态或距离超过"+ SPUtil.getIntValue(SPUtil.STOP_MISSION_SET_DISTANCE)+"米，不允许结束任务！");
                        }
                    }
                }).build();
        //if (dialog)
        if(dialog.isCancelled()) {
            ViewUtil.removeHomeKeyListener(dialog);
        }
    }

}
